package unit6;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Random;

public class ProxyTest {
	public static void main(String[] args) {
		Object[] obj=new Object[100];
		for(int x=0;x<obj.length;x++){
			Integer value=x+1;
			InvocationHandler handler=new TraceHandler(value);
			Object proxy=Proxy.newProxyInstance(null, new Class[]{Comparable.class}, handler);
			obj[x]=proxy;
		}
		
		Integer key=new Random().nextInt(obj.length)+1;
		
		int result=Arrays.binarySearch(obj, key);
		
		if(	result>=0)
			System.out.println(obj[result]);
	}
	
}
class TraceHandler implements InvocationHandler{
	private Object target;
	
	
	public TraceHandler(Object obj) {
		// TODO Auto-generated constructor stub
		this.target=obj;
	}


	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// TODO Auto-generated method stub
		System.out.print(target);
		System.out.print("."+method.getName()+"(");
		if(args!=null){
			for(int x=0;x<args.length;x++){
				System.out.print(args[x]);
				if(x<args.length-1)
					System.out.print(", ");
			}
		}
		System.out.println(")");
		return method.invoke(target, args);
	}
	
}